package com.tulingxueyuan.mall.modules.pms.service.impl;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tulingxueyuan.mall.modules.pms.mapper.PmsProductCategoryAttributeRelationMapper;
import com.tulingxueyuan.mall.modules.pms.model.PmsProductCategory;
import com.tulingxueyuan.mall.modules.pms.mapper.PmsProductCategoryMapper;
import com.tulingxueyuan.mall.modules.pms.model.PmsProductCategoryAttributeRelation;
import com.tulingxueyuan.mall.modules.pms.model.dto.PmsProductCategoryDTO;
import com.tulingxueyuan.mall.modules.pms.model.dto.ProductCateChildernDTO;
import com.tulingxueyuan.mall.modules.pms.service.PmsProductCategoryAttributeRelationService;
import com.tulingxueyuan.mall.modules.pms.service.PmsProductCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.beans.Transient;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 产品分类 服务实现类
 * </p>
 *
 * @author XuShu
 * @since 2022-02-24
 */
@Service
public class PmsProductCategoryServiceImpl extends ServiceImpl<PmsProductCategoryMapper, PmsProductCategory> implements PmsProductCategoryService {

    @Autowired
  private PmsProductCategoryAttributeRelationService service;

    @Autowired
    private PmsProductCategoryMapper mapper;


    /**
     * 获取商品分类列表
     * @param parentId
     * @param pageNum
     * @param pageSize
     * @return
     */

    @Override
    public Page list(long parentId, Integer pageNum, Integer pageSize) {
        Page page =new Page(pageNum,pageSize);
        //条件构造器
        QueryWrapper<PmsProductCategory> queryWrapper =new QueryWrapper<>();
        queryWrapper.lambda().eq(PmsProductCategory::getParentId,parentId)
                .orderByAsc(PmsProductCategory::getSort);

        return this.page(page, queryWrapper);

        
    }

    /**
     * 修改导航栏显示状态
     * @param ids
     * @param navStatus
     */
    @Override
    public boolean updateNavStatus(List<Long> ids, Integer navStatus) {
        UpdateWrapper<PmsProductCategory> pmsProductCategoryUpdateWrapper = new UpdateWrapper<>();
        //需要更新的列
        pmsProductCategoryUpdateWrapper.lambda().set(PmsProductCategory::getNavStatus,navStatus)
                .in(PmsProductCategory::getId,ids);

      return   this.update(pmsProductCategoryUpdateWrapper);

    }

    @Override
    public boolean updateShowStatus(List<Long> ids, Integer showStatus) {
        UpdateWrapper<PmsProductCategory> pmsProductCategoryUpdateWrapper = new UpdateWrapper<>();
        //需要更新的列
        pmsProductCategoryUpdateWrapper.lambda().set(PmsProductCategory::getShowStatus,showStatus)
                .in(PmsProductCategory::getId,ids);

        return   this.update(pmsProductCategoryUpdateWrapper);

    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean customSave(PmsProductCategoryDTO productCategoryDTO) {

        //保存商品分类
        PmsProductCategory productCategory = new PmsProductCategory();
        //通过spring工具类将传输数据的对象拷贝到商品分类中
        BeanUtils.copyProperties(productCategoryDTO,productCategory);
        //由于商品数量 和级别 在表单中没有维护 需要设置默认值
        productCategory.setProductCount(0);
        if (productCategory.getParentId()==0){
            productCategory.setLevel(0);
        }else {
            productCategory.setLevel(1);
        }
        this.save(productCategory);
        saveAttrRelation(productCategoryDTO, productCategory);
        return true;

    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean update(PmsProductCategoryDTO productCategoryDTO) {
        boolean isSaveI,isSaveII;
        //保存商品分类
        PmsProductCategory productCategory = new PmsProductCategory();
        //通过spring工具类将传输数据的对象拷贝到商品分类中
        BeanUtils.copyProperties(productCategoryDTO,productCategory);
        if (productCategory.getParentId()==0){
            productCategory.setLevel(0);
        }else {
            productCategory.setLevel(1);
        }
        isSaveI= this.updateById(productCategory);
        QueryWrapper<PmsProductCategoryAttributeRelation> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(PmsProductCategoryAttributeRelation::getProductCategoryId,productCategory.getId());
        service.remove(queryWrapper);

        isSaveII = saveAttrRelation(productCategoryDTO, productCategory);
        return true;
    }



    /**
     * 添加关联属性
     * @param productCategoryDTO
     * @param productCategory
     * @return
     */
    private boolean saveAttrRelation(PmsProductCategoryDTO productCategoryDTO, PmsProductCategory productCategory) {
        List<Long> productAttributeIdList = productCategoryDTO.getProductAttributeIdList();
        List<PmsProductCategoryAttributeRelation> list = new ArrayList<>();
        for (Long attrId : productAttributeIdList) {
            //得到分类保存后的主键id 保存商品分类筛选属性关系
            PmsProductCategoryAttributeRelation productCategoryAttributeRelation = new PmsProductCategoryAttributeRelation();
            productCategoryAttributeRelation.setProductCategoryId(productCategory.getId());
            productCategoryAttributeRelation.setProductAttributeId(attrId);
            list.add(productCategoryAttributeRelation);
        }
        return service.saveBatch(list);

    }

    /**
     * 获取商品一级分类和二级分类
     * @return
     */
    @Override
    public List<ProductCateChildernDTO> getWithChildren() {
        return mapper.getWithChildren();
    }

}



